import { post } from "../../../utils/request";
import ResourceTreeNode from "../menu/item/tree-node";
import utils from "../../../utils/utils";
const env = import.meta.env;

const apiMenu = {
  queryMenuListing(data?: object) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/menu.listing",
      data
    );
  },
  getMenu(menuId: string) {
    return post(env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/menu.get", {
      menuId: menuId,
    });
  },
  updateMenu(data?: object) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/menu.update",
      data
    );
  },
  deleteMenu(menuId: string) {
    return post(env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/menu.delete", {
      menuId,
    });
  },
  queryMenuItemListing(data?: object) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/menu.item.listing",
      data
    );
  },
  getMenuItem(menuItemId: string) {
    return post(env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/menu.item.get", {
      menuItemId: menuItemId,
    });
  },
  updateMenuItem(data?: object) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/menu.item.update",
      data
    );
  },
  deleteMenuItem(menuItemId: string) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/menu.item.delete",
      { menuItemId }
    );
  },
  queryMenuItemChildrenListing(parentId: string) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/menu.item.children.listing",
      {
        parentId,
      }
    );
  },
  /**
   *
   * @param parentId
   * @returns
   */
  async loadChilds(parentId: string): Promise<ResourceTreeNode[]> {
    let treeData = Array<ResourceTreeNode>();
    await apiMenu
      .queryMenuItemChildrenListing(parentId)
      .then((response: any) => {
        if (response.data.code == 200) {
          if (response.data.data != "") {
            if (response.data.data != "") {
              treeData = utils.jsonArray2TypescriptArray(response.data.data);
            }
          }
        }
      })
      .catch((err: any) => {
        utils.errorNotification("数据处理错误", err);
      });

    return treeData;
  },
};

export default apiMenu;
